<div class="hbox hbox-auto-xs hbox-auto-sm" ng-controller="AbnTestController">
   <div class="col w-lg bg-light b-r bg-auto">
    <div class="wrapper-md"> 

       <h5>部门管理:</h5>
       <div class="line b-b line-lg"></div> 
     <!-- <button ng-click="my_tree.select_first_branch()" class="btn btn-default btn-sm">选中第一行</button>
      <div class="m-t-sm m-b-sm">
        <button ng-click="my_tree.select_next_sibling()" class="btn btn-default btn-sm">下一个父级</button>
        <button ng-click="my_tree.select_prev_sibling()" class="btn btn-default btn-sm">上一个父级</button>
      </div>
      <div class="m-b-sm">
        <button ng-click="my_tree.select_next_branch()" class="btn btn-default btn-sm">下一级</button>
        <button ng-click="my_tree.select_prev_branch()" class="btn btn-default btn-sm">上一级</button>
      </div>
      <button ng-click="my_tree.select_parent_branch()" class="btn btn-default btn-sm">父级</button>
      <div class="m-t-sm m-b-sm">
        <button ng-click="my_tree.expand_branch()" class="btn btn-default btn-sm">展开当前菜单</button>
        <button ng-click="my_tree.collapse_branch()" class="btn btn-default btn-sm">关闭当前菜单</button>
      </div>-->
      <div class="m-t-sm m-b-sm">
        <button ng-click="my_tree.expand_all()" class="btn btn-default btn-sm">展开全部</button>
        <button ng-click="my_tree.collapse_all()" class="btn btn-default btn-sm">关闭全部</button>
      </div>
      
      <button ng-click="addTreeNode()" class="btn btn-success btn-sm"><i class="fa fa-fw m-r-xs fa-plus"></i> 新增</button>
      <button ng-click="editTreeNode()" class="btn btn-warning btn-sm" ><i class="fa fa-fw m-r-xs fa-plus"></i> 修改</button>
      <button ng-click="removeTreeNode()" class="btn btn-danger btn-sm" ><i class="fa fa-fw m-r-xs fa-plus"></i> 刪除</button>

    
    </div>
  </div>
  <div class="col">
    <div class="wrapper-md">


       <!--隐藏ztree编辑页-->
  <div id="ztreeEditDiv" style="display: none;" >
    <div class="wrapper-md"  >
      <div class="panel panel-default">
        <div>
          <div class="row">
            <div class="col-sm-12" >
              <form name="form-contact" class="form-horizontal"  ng-submit="processForm()">
                <div class="panel-body">
                 
                  <div class="form-group">
                    <label class="col-sm-3 control-label">上级菜单</label>
                    <div class="col-sm-9">

                      
                      <div class="col-sm-9" style="margin-top: 6px;">
                        <span>{{father.ClassName}}</span>
                      </div>

                    </div>
                  </div>
                  <div class="line line-dashed b-b line-lg pull-in"></div>

                  <div class="form-group">
                    <label class="col-sm-3 control-label">分类名称</label>
                    <div class="col-sm-9">
                     <input type="text" class="form-control" placeholder="分类名称" ng-model="contact.ClassName" required >
                   </div>
                 </div>
                 <div class="line line-dashed b-b line-lg pull-in"></div>


                 <div class="form-group">
                  <label class="col-sm-3 control-label">图标</label>
                  <div class="col-sm-9">
                   <input type="text" class="form-control" placeholder="图标" ng-model="contact.ClassSmallIcon"  >
                 </div>
               </div>

               <div class="line line-dashed b-b line-lg pull-in"></div>

               <div class="form-group">
                <label class="col-sm-3 control-label">备注</label>
                <div class="col-sm-9">
                 <input type="text" class="form-control" placeholder="备注" ng-model="contact.ClassRemark"  >
               </div>
             </div>
             <div class="line line-dashed b-b line-lg pull-in"></div>

             <div class="form-group">
              <label class="col-sm-3 control-label">排序</label>
              <div class="col-sm-9">
               <input type="Number" class="form-control" placeholder="排序" ng-model="contact.ClassDisplayOrder" required >
             </div>
           </div>

           <!--隐藏字段-->
           <input type="hidden"  ng-model="contact.Id"  >
           <input type="hidden"  ng-model="contact.FatherId"  >
           <input type="hidden"  ng-model="contact.ClassUuid"  >
           <input type="hidden"  ng-model="contact.AdderId"  >
           <input type="hidden"  ng-model="contact.AdderName"  >
           <input type="hidden"  ng-model="contact.AddTime"  >
           <input type="hidden"  ng-model="contact.UpdaterId"  >
           <input type="hidden"  ng-model="contact.UpdaterName"  >
           <input type="hidden"  ng-model="contact.UpdateTime"  >
           <input type="hidden"  ng-model="contact.DelFlag" ng-init="contact.DelFlag='0'" >

           <!--隐藏字段-->
           <footer class="panel-footer text-right bg-light lter">
            <button type="submit" class="btn btn-success">提交</button>
          </footer>

        </div>
      </form>
    </div>
  </div>
</div>
</div>
</div>
<!--隐藏ztree编辑页-->


</div>

      <div class="b-a bg-light dk r r-2x">
        <span ng-if="doing_async">...loading...</span>
        <abn-tree 
        tree-data         = "my_data" 
        tree-control      = "my_tree" 
        on-select         = "my_tree_handler(branch)" 
        expand-level      = "2" 
        initial-selection = "Granny Smith"
        icon-leaf         = "fa fa-file-o"
        icon-expand       = "fa fa-plus"
        icon-collapse     = "fa fa-minus"
        >
      </abn-tree>
    </div>
  </div>
</div>

</div>

<script type="text/javascript">
  app.controller('AbnTestController', function($scope, $timeout,$state) {
    var apple_selected, tree, treedata_avm, treedata_geography;
    var menuTopUrl = API_URL+"user_class?query=delflag:0,FatherId:0&number="+Math.random();
    var data = MY_ASYNC_AJAX(menuTopUrl);
    var temps =  [];
    for (var i = data.length - 1; i >= 0; i--) {
      var obj =  { label: data[i].ClassName, pId: data[i].FatherId ,Id:data[i].Id,children:getChildren(data[i].Id)}
      temps.push(obj);
    }
      //递归获取子级
      function getChildren(classId){
        var url = API_URL+"user_class?query=delflag:0,FatherId:"+classId+"&number="+Math.random();
        var data = MY_ASYNC_AJAX(url);
        var temps =  [];
        if(null!=data){
         for (var i = data.length - 1; i >= 0; i--) {
          var obj =  { label: data[i].ClassName, pId: data[i].FatherId ,Id:data[i].Id,children:getChildren(data[i].Id) }
          temps.push(obj);
        }
        return temps;
      }
    }

    //赋值
    $scope.my_data = temps;

    $scope.my_tree = tree = {};

    //添加节点
$scope.addTreeNode=function(){
  var selected;
      //得到了父级信息
      selected = tree.get_selected_branch();
      if(selected!=null&&selected!=''){
      var id = selected.Id;
      }

  if(id!=''&&id!=undefined&&id!=null){
   var fatherUrl  =  API_URL +"user_class?query=delflag:0,Id:"+id;
   var father = MY_ASYNC_AJAX(fatherUrl)[0];
   $scope.father =  father;
   $scope.contact.FatherId = father.Id;
 }else{
   $scope.contact.FatherId = 0 ;
   $scope.father= {Id:0,ClassName:'顶级菜单'};
 }

 delete $scope.contact['Id'];  
 
 top.layer.open({
  type: 1,
  title: '输入分类信息',
  fix: false,
        maxmin: true, //开启最大化最小化按钮
        shadeClose: true,
        shift: 4,
        shade: [0.5],
        //skin: 'layui-layer-rim',边框
        move: false,
        area: ['600px', '510px'],
        content: $('#ztreeEditDiv'),

     });
}

//修改操作
$scope.editTreeNode = function(){
  var selected;
    //得到了選中信息
    selected = tree.get_selected_branch();
    if(selected!=null&&selected!=''){
    var id = selected.Id;
    }else{
      top.layer.msg('请选择需要修改的数据', {icon: 6});
      return;
    }
  var znUrl  =  API_URL +"user_class?query=delflag:0,id:"+id;
 var detailTree  = MY_ASYNC_AJAX(znUrl);
 detailTree[0].ClassDisplayOrder = Number(detailTree[0].ClassDisplayOrder);
 $scope.contact =   detailTree[0];
 if(detailTree[0].FatherId>0){
   var fatherUrl  =  API_URL +"user_class?query=delflag:0,Id:"+ detailTree[0].FatherId;

   var father = MY_ASYNC_AJAX(fatherUrl)[0];

   $scope.father =   father;
   $scope.contact.FatherId = father.Id;
 }else{
   $scope.contact.FatherId = 0 ;
   $scope.father= {Id:0,ClassName:'顶级菜单'};
 }

 top.layer.open({
   type: 1,
   title: '输入栏目信息',
   fix: false,
       maxmin: true, //开启最大化最小化按钮
       shadeClose: true,
       shift: 3,
       shade: [0.5],
       //skin: 'layui-layer-rim',边框
       maxmin: true,
       move: false,
       area: ['600px', '510px'],
       content: $('#ztreeEditDiv'),
       shadeClose: false,
        
   });



}

//刪除操作
//删除节点
$scope.removeTreeNode=function(){
   var selected;
    //得到了選中信息
    selected = tree.get_selected_branch();
    if(selected!=null&&selected!=''){
    var id = selected.Id;
    }else{
      top.layer.msg('请选择需要删除的数据', {icon: 6});
      return;
    }
  top.layer.confirm('您确定删除吗', function(index){
    var detailUrl  =  API_URL +"user_class?query=delflag:0,Id:"+ id;
    var detail = MY_ASYNC_AJAX(detailUrl);
    var formData = detail[0];
    formData.delflag = '1';
    var delUrl =   API_URL+"user_class";
    $.ajax({  
        async : false, //是否异步  
        cache : false, //是否使用缓存  
        type : 'put', //请求方式,post  
        dataType : "json", //数据传输格式  
        url : delUrl+'/'+id, //请求链接  
        data: JSON.stringify(formData),
        error : function() {  
          console.log('亲，网络有点不给力呀！');  
        },  
        success : function(data) { 
          top.layer.msg('删除成功', {icon: 6});
            
        
      }  
    }); 
        top.layer.close(index);
        window.location.reload();
     
  });

}



  //表单提交开始
$scope.processForm= function (){
  //1.参数处理
//发起POST请求
var postInfoUrl = API_URL+"user_class";
var formData =  $scope.contact;

  //1.参数处理
  formData.AddTime = getNowDBFormatDate();
  formData.UpdateTime = getNowDBFormatDate();
  formData.ClassLevel =  '1';
  formData.AdderId = 1;
  formData.AdderName = '1';
  formData.UpdaterId = 1;
  formData.UpdaterName = '1';
  formData.ClassUuid='';
  formData.ClassDisplayOrder =  formData.ClassDisplayOrder+'';
  console.log("formData.ClassDisplayOrder"+formData.ClassDisplayOrder)

  console.log(JSON.stringify(formData));


  if("undefined" != typeof(formData.Id)){
    $.ajax( {  
      async : false, //是否异步  
      cache : false, //是否使用缓存  
      type : 'put', //请求方式,post  
      dataType : "json", //数据传输格式  
      url : postInfoUrl+'/'+formData.Id, //请求链接  
      data: JSON.stringify(formData),
      error : function() {  
        console.log('亲，网络有点不给力呀！');  
      },  
      success : function(data) { 
         top.layer.msg('修改成功', {icon: 6});
      }  
    }); 

  }else{

    $.ajax( {  
        async : false, //是否异步  
        cache : false, //是否使用缓存  
        type : 'post', //请求方式,post  
        dataType : "json", //数据传输格式  
        url : postInfoUrl, //请求链接  
        data: JSON.stringify(formData),
        error : function() {  
          console.log('亲，网络有点不给力呀！');  
        },  
        success : function(data) { 
         top.layer.msg('新增成功', {icon: 6});
       }  
     }); 

  }

   layer.closeAll();

   window.location.reload();  
  
}

 //表单提交结束







});










</script>